package com.oracle.oa.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONSerializer;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.CalendarBiz;
import com.oracle.oa.biz.ScheduleBiz;
import com.oracle.oa.entity.Calendar;
import com.oracle.oa.entity.Schedule;
import com.oracle.oa.entity.Userinfo;
import com.oracle.oa.util.MyCalendar;

public class JsonAction extends ActionSupport implements SessionAware,ServletResponseAware {
	private HttpServletResponse response;
	private String result;
	CalendarBiz calbiz;
	ScheduleBiz sbiz;
	Map<String, Object> session;
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 从数据库查找数据向index.jsp页面返回json字符数组
	 * @throws IOException
	 */
	
	public void doJson() throws IOException{
		String json="";
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		//List<Calendar> list = calbiz.getAll(Calendar.class); //查询所有记录到日历
		//-----------------查询当前用户的日程信息输出到日历上-------------------------------------
		List<Calendar> list =new ArrayList<Calendar>();
		Userinfo uf=(Userinfo) session.get("userinfo");
		List<Schedule> schedule = (List<Schedule>) sbiz.getAllByHQL("from Schedule where userinfo="+uf.getUserid());
		for (int i = 0; i < schedule.size(); i++) {
			Calendar c=schedule.get(i).getCalendar();
			list.add(c);
			System.out.println(list.size());
		}
		//-----------------查询结束-----------------------------------------------------------
		System.out.println("Calendar的条数"+list.size());
		List<MyCalendar> listm=new ArrayList<MyCalendar>();
		
		for (Calendar calendar : list) {
			//System.out.println(calendar.getId1()+"\t"+calendar.getStart1()+"\t"+calendar.getEnd1()+"\t"+calendar.getAllday()+"\t"+calendar.getColor()+"\t");
			MyCalendar m=new MyCalendar();
			m.setAllday(calendar.getAllday());
			m.setColor(calendar.getColor());
			m.setEnd(calendar.getEnd());
			m.setStart(calendar.getStart());
			m.setId(calendar.getId());
			m.setTitle(calendar.getTitle());
			listm.add(m);
		}
		
		try {
			json = JSONSerializer.toJSON(listm).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  result=json;
		  System.out.println(result);
		  out.print(result);
		  out.flush();
		  out.close();
		//值得一提的是，在返回的json数据中，每个字段如id，title..对应着FullCalendar的Event Object事件对象中的属性id，title..。
	}
	
	public void setSession(Map<String, Object> arg0) {
		this.session=arg0;
	}

	public CalendarBiz getCalbiz() {
		return calbiz;
	}

	public void setCalbiz(CalendarBiz calbiz) {
		this.calbiz = calbiz;
	}

	public void setServletResponse(HttpServletResponse arg0) {
		this.response=arg0;
		
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}


	public ScheduleBiz getSbiz() {
		return sbiz;
	}


	public void setSbiz(ScheduleBiz sbiz) {
		this.sbiz = sbiz;
	}

}
